
package com.example.filetransfer.publicsource.connection;

public class Protocol {
    public static String SYNCDIR = "E:\\music";
    public static String FILETREENAME = ".filetree.xml";
    public static final String COMMAND_FILE_TRANSFER_START = "_file_transfer_start";
    public static final String COMMAND_FILE_TRANSFER_END = "_file_transfer_end";
    public static final String COMMAND_NEED_GET_CLIENT_FILE = "_send_me_your_files";
    public static final String COMMAND_NEED_GET_CLIENT_DIFF_FILES = "_send_me_your_diff_files";
    public static final String COMMAND_NEED_SEND_TO_CLIENT = "_send_files_to_you";
    public static final String COMMAND_SEND_ME_YOUR_FILETREE = "_send_me_your_filetree";
    public static final String COMMAND_SEND_ME_YOUR_FILE_VERSION = "_send_me_file_version";

    public static final String REPLY_PREFIX = "_reply_";
    public static final String REPLY_MESSAGE_FILETREE = REPLY_PREFIX + "filetree_sended";
    public static final String REPLY_MESSAGE_SEND_FILE_END = REPLY_PREFIX + "send_file_end:";
    public static final String REPLY_MESSAGE_FILE_NO_CHANGE = REPLY_PREFIX + "filetree_no_change";

    public static final String REPLY_MESSAGE_FILE_VERSION = REPLY_PREFIX + "fileversion:";

    static final String COMMAND_SOCKET_DISCONNECT = "_disconnect";
    static final String FILE_PREFIX = "file:";
    static final String MESSAGE_PREFIX = "message:";

    public static String createFileSendEndMessage(String version) {
        return REPLY_MESSAGE_SEND_FILE_END + version;
    }

    public static String getFileSendEndVersion(String message) {
        return message.substring(REPLY_MESSAGE_SEND_FILE_END.length());
    }

    public static String createFileVersionMessage(String version) {
        return REPLY_MESSAGE_FILE_VERSION + version;
    }

    public static String getFileVersionFromMessage(String message) {
        return message.substring(REPLY_MESSAGE_FILE_VERSION.length());
    }

    static boolean sendFile(String data) {
        return data.startsWith(FILE_PREFIX);
    }

    static boolean sendMessage(String data) {
        return data.startsWith(MESSAGE_PREFIX);
    }

    static String realURL(String encodedUri) {
        return encodedUri.substring(FILE_PREFIX.length());
    }

    public static String removeFileRoot(String root, String url) {
        // return url;
        return buildFileUrl(realURL(url).substring(root.length()));
    }

    static String realData(String encodedUri) {
        return encodedUri.substring(MESSAGE_PREFIX.length());
    }

    public static String buildFileUrl(String data) {
        return FILE_PREFIX + data;
    }

    static String buildMessage(String data) {
        return MESSAGE_PREFIX + data;
    }

    public static final int BROADCAST_CLIENT_LISTEN_PORT = 10087;
    public static final int BROADCAST_SERVER_LISTEN_PORT = 10088;
    public static final int DEFAULT_SERVER_PORT = 10089;
    public static final String MESSAGE_SEND_SERVER_ADDRESS = "send_me_server_address";
    public static final String MESSAGE_SERVER_ADDRESS = "server_address:%s:%d";
    public static final String MESSAGE_SEND_ME_YOUR_ALL_FILES = "_request_send_me_all_your_files";
    public static final String REPLY_SEND_FILE_DONE_PREFIX = "_reply_send_file_done:";
    public static final String MESSAGE_SEND_ME_YOUR_FILE_VERSION = "_request_send_me_file_version";
    public static final String REPLY_MY_FILE_VERSION = "_reply_file_version:";
    public static final String MESSAGE_SEND_MY_FILETREE_DONE = "_request_send_my_file_tree_done";
    public static final String MESSAGE_FILE_NEED_SEND_ME = "_reply_send_me_your_files:";
    public static final String MESSAGE_SYNC_DONE = "sync_done";
    public static final String MESSAGE_CLIENT_NEW = "phone_is_more_new";
    public static final String MESSAGE_SERVER_NEW = "server_is_more_new";
    public static final String RELY_MESSAGE_SYNC_DONE = "_reply_sync_done";
    public static final String FILE_SPLITER = "send;";

    public static String createServerAddress(String ip, int port) {
        return String.format(MESSAGE_SERVER_ADDRESS, ip, port);
    }

    public static boolean replySendFileToMe(String message) {
        return message == null ? false : message.startsWith(MESSAGE_FILE_NEED_SEND_ME);
    }

    public static boolean replyFileSendDone(String message) {
        return message == null ? false : message.startsWith(REPLY_SEND_FILE_DONE_PREFIX);
    }

    public static boolean replyFileVersion(String message) {
        return message == null ? false : message.startsWith(REPLY_MY_FILE_VERSION);
    }

    public static String getIpFromAddressMessage(String message) {
        // TODO Regex here
        int start = message.indexOf(":");
        int end = message.lastIndexOf(":");
        if (end > start) {
            return message.substring(start + 1, end);
        } else {
            throw new IllegalArgumentException("Error message format");
        }
    }

    public static int getPortFromAddressMessage(String message) {
        // TODO Regex here
        int start = message.indexOf(":");
        int end = message.lastIndexOf(":");
        if (end > start) {
            return Integer.parseInt(message.substring(end + 1));
        } else {
            throw new IllegalArgumentException("Error message format");
        }
    }
}
